package cn.edu.scau.cmi.dengshixin;

import java.net.URL;
import java.util.ResourceBundle;

import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;

import java.io.InputStream;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.edu.scau.cmi.dengshixin.dao.AdvisorDAO;
import cn.edu.scau.cmi.dengshixin.dao.StudentDAO;
import cn.edu.scau.cmi.dengshixin.dao.TeacherDAO;
import cn.edu.scau.cmi.dengshixin.domain.Advisor;
import cn.edu.scau.cmi.dengshixin.domain.Student;
import cn.edu.scau.cmi.dengshixin.domain.Teacher;
import java.util.List;

public class Controller {
	
	private static InputStream in;
	private static SqlSessionFactory factory;
	private static SqlSession session;
	private AdvisorDAO advDAO;
	private StudentDAO stuDAO;
	private TeacherDAO teaDAO;
	
	private void init() throws Exception {
		in = Resources.getResourceAsStream("SqlMapConfig.xml");
		factory = new SqlSessionFactoryBuilder().build(in);
		session = factory.openSession();
		advDAO = session.getMapper(AdvisorDAO.class);
		stuDAO = session.getMapper(StudentDAO.class);
		teaDAO = session.getMapper(TeacherDAO.class);
//		ApplicationContext applicationContext = getApplicationContext1();
//		advDAO = applicationContext.getBean(AdvisorDAO.class);
//		stuDAO = applicationContext.getBean(StudentDAO.class);
//		teaDAO = applicationContext.getBean(TeacherDAO.class);
	}
	
	private  ApplicationContext getApplicationContext1() {
		ApplicationContext ac1 = new ClassPathXmlApplicationContext("applicationContext.xml");
		return ac1;
	}
	
	public static void destroy() throws Exception {
		session.commit();
		session.close();
		in.close();
	}

    @FXML
    private ResourceBundle resources;

    @FXML
    private URL location;

    @FXML
    private TextField addTeacherIdTF;

    @FXML
    private Button addStudentBtn;

    @FXML
    private TextField modifyTeacherIdTF;

    @FXML
    private TextArea resultArea;

    @FXML
    private TextField addAdvisorTeacherIdTF;

    @FXML
    private Button findTeacherByIdBtn;

    @FXML
    private TextField addTeacherNameTF;

    @FXML
    private TextField addStudentIdTF;

    @FXML
    private Button deleteTeacherBtn;

    @FXML
    private Button findAllAdvisorsBtn;

    @FXML
    private Button addTeacherBtn;

    @FXML
    private Button findAllTeachersBtn;

    @FXML
    private TextField delStudentByIdTF;

    @FXML
    private Button modifyStudentIdBtn;

    @FXML
    private Button findAllStudentsBtn;

    @FXML
    private TextField findStudentIdTF;

    @FXML
    private Button deleteStudentBtn;

    @FXML
    private Button findStudentByIdBtn;

    @FXML
    private TextField addAdvisorStudentIdTF;

    @FXML
    private TextField modifyStudentIdTF;

    @FXML
    private TextField deTeacherByNameTF;

    @FXML
    private Button studentReleaseBtn;

    @FXML
    private Button teacherReleaseBtn;

    @FXML
    private TextField findTeacherIdTF;

    @FXML
    private TextField addStudentNameTF;

    @FXML
    private Button addAdvisorBtn;

    @FXML
    private Button modifyTeacherIdBtn;

    @FXML
    void findAllStu(ActionEvent event) throws Exception {
    	List<Student> students = stuDAO.getStudents();
    	System.out.print("ok\n");
    	if(students.isEmpty()) {
    		resultArea.appendText("查询学生结果为空\n");
    		return;
    	}
    	resultArea.appendText("查询学生结果为：\n");
    	for(Student each : students) {
    		resultArea.appendText(each.toString());
    	}
    }

    @FXML
    void findAllTea(ActionEvent event) throws Exception {
    	List<Teacher> teachers = teaDAO.getTeachers();
    	if(teachers.isEmpty()) {
    		resultArea.appendText("查询教师结果为空\n");
    		return;
    	}
    	resultArea.appendText("查询教师结果为：\n");
    	for(Teacher each : teachers) {
    		resultArea.appendText(each.toString());
    	}
    }

    @FXML
    void findAllAdv(ActionEvent event) throws Exception {
    	List<Advisor> advisors = advDAO.getAdvisors();
    	if(advisors.isEmpty()) {
    		resultArea.appendText("查询指导关系结果为空\n");
    		return;
    	}
    	resultArea.appendText("查询指导关系结果为：\n");
    	for(Advisor each : advisors) {
    		resultArea.appendText(each.toString());
    	}
    }

    @FXML
    void addStu(ActionEvent event) throws Exception {
    	String id = addStudentIdTF.getText();
    	String name = addStudentNameTF.getText();
    	if(id.equals("") || name.equals("")) {
    		resultArea.appendText("学生姓名或ID不能为空！\n");
    		return;
    	}
    	Student stu = new Student(id, name);
    	stuDAO.addStudent(stu);
    	session.commit();
    	resultArea.appendText("插入学生成功！\n");
    }

    @FXML
    void deleteStu(ActionEvent event) throws Exception {
    	String id = delStudentByIdTF.getText();
    	if(id.equals("")) {
    		resultArea.appendText("学生ID不能为空！\n");
    		return;
    	}
    	stuDAO.deleteById(id);
    	session.commit();
    	session.close();
    	in.close();
    	resultArea.appendText("删除学生成功！\n");
    }

    @FXML
    void deleteTea(ActionEvent event) throws Exception {
    	String name = deTeacherByNameTF.getText();
    	if(name.equals("")) {
    		resultArea.appendText("教师姓名不能为空！\n");
    		return;
    	}
    	teaDAO.deleteByName(name);
    	session.commit();
    	resultArea.appendText("删除教师成功！\n");
    }

    @FXML
    void findStu(ActionEvent event) throws Exception {
    	String stu_id = findStudentIdTF.getText();
    	if(stu_id.equals("")) {
    		resultArea.appendText("学生ID不能为空！\n");
    		return;
    	}
    	Advisor adv = advDAO.findAdvByStuID(stu_id);
    	if(adv == null) {
    		resultArea.appendText("该学生没有指导教师\n");
    	}
    	else {
    		resultArea.appendText(adv.toString());
    	}
    }

    @FXML
    void modifyTea(ActionEvent event) throws Exception {
    	String stu_id = findStudentIdTF.getText();
    	if(stu_id.equals("")) {
    		resultArea.appendText("学生ID不能为空！\n");
    		return;
    	}
    	Advisor adv = advDAO.findAdvByStuID(stu_id);
    	if(adv == null) {
    		resultArea.appendText("该学生不存在指导关系\n");
    	}
    	else {
    		String tea_id = modifyTeacherIdTF.getText();
    		if(tea_id.equals("")) {
    			resultArea.appendText("请输入需要更改或解除关系的指导老师ID！\n");
    			return;
    		}
    		Teacher tea = teaDAO.findTeacher(tea_id);
    		if(tea == null) {
    			resultArea.appendText("请正确输入指导老师ID！\n");
    			List<Teacher> teachers = teaDAO.getTeachers();
    			for(Teacher each : teachers) {
    				resultArea.appendText(each.toString());
    			}
    			return;
    		}
    		Advisor mod_adv = new Advisor(stu_id, tea_id);
    		advDAO.modifyAdvisor(mod_adv);
    		session.commit();
    		resultArea.appendText("修改指导老师成功！\n");
    	}
    }

    @FXML
    void stuRelease(ActionEvent event) throws Exception {
    	String stu_id = findStudentIdTF.getText();
    	if(stu_id.equals("")) {
    		resultArea.appendText("学生ID不能为空！\n");
    		return;
    	}
    	Advisor adv = advDAO.findAdvByStuID(stu_id);
    	if(adv == null) {
    		resultArea.appendText("该学生不存在指导关系\n");
    	}
    	else {
    		String tea_id = modifyTeacherIdTF.getText();
    		if(tea_id.equals("")) {
    			resultArea.appendText("请输入需要更改或解除关系的指导老师ID！\n");
    			return;
    		}
    		Teacher tea = teaDAO.findTeacher(tea_id);
    		if(tea == null) {
    			resultArea.appendText("请正确输入指导老师ID！\n");
    			List<Teacher> teachers = teaDAO.getTeachers();
    			for(Teacher each : teachers) {
    				resultArea.appendText(each.toString());
    			}
    			return;
    		}
    		Advisor del_adv = new Advisor(stu_id, tea_id);
    		advDAO.delAdvisor(del_adv);
    		session.commit();
    		resultArea.appendText("解除指导关系成功！\n");
    	}
    }

    @FXML
    void findTea(ActionEvent event) throws Exception {
    	String tea_id = findTeacherIdTF.getText();
    	if(tea_id.equals("")) {
    		resultArea.appendText("教师ID不能为空！\n");
    		return;
    	}
    	Advisor adv = advDAO.findAdvByTeaID(tea_id);
    	if(adv == null) {
    		resultArea.appendText("该教师没有指导学生\n");
    	}
    	else {
    		resultArea.appendText(adv.toString());
    	}
    }

    @FXML
    void modifyStu(ActionEvent event) throws Exception {
    	String tea_id = findTeacherIdTF.getText();
    	if(tea_id.equals("")) {
    		resultArea.appendText("教师ID不能为空！\n");
    		return;
    	}
    	Advisor adv = advDAO.findAdvByTeaID(tea_id);
    	if(adv == null) {
    		resultArea.appendText("该教师没有指导学生\n");
    	}
    	else {
    		String stu_id = modifyStudentIdTF.getText();
    		if(stu_id.equals("")) {
    			resultArea.appendText("请输入需要更改或解除关系的指导学生ID！\n");
    			return;
    		}
    		Student stu = stuDAO.findStudent(stu_id);
    		if(stu == null) {
    			resultArea.appendText("请正确输入指导学生ID！\n");
    			List<Student> students = stuDAO.getStudents();
    			for(Student each : students) {
    				resultArea.appendText(each.toString());
    			}
    			return;
    		}
    		Advisor mod_adv = new Advisor(stu_id, tea_id);
    		advDAO.modifyAdvisor(mod_adv);
    		session.commit();
    		resultArea.appendText("修改指导学生成功！\n");
    	}
    }

    @FXML
    void teaRelease(ActionEvent event) throws Exception {
    	String tea_id = findTeacherIdTF.getText();
    	if(tea_id.equals("")) {
    		resultArea.appendText("教师ID不能为空！\n");
    		return;
    	}
    	Advisor adv = advDAO.findAdvByTeaID(tea_id);
    	if(adv == null) {
    		resultArea.appendText("该教师没有指导学生\n");
    	}
    	else {
    		String stu_id = modifyStudentIdTF.getText();
    		if(stu_id.equals("")) {
    			resultArea.appendText("请输入需要更改或解除关系的指导学生ID！\n");
    			return;
    		}
    		Student stu = stuDAO.findStudent(stu_id);
    		System.out.println("ok");
    		if(stu == null) {
    			resultArea.appendText("请正确输入指导学生ID！\n");
    			List<Student> students = stuDAO.getStudents();
    			for(Student each : students) {
    				resultArea.appendText(each.toString());
    			}
    			return;
    		}
    		Advisor del_adv = new Advisor(stu_id, tea_id);
    		advDAO.delAdvisor(del_adv);
    		session.commit();
    		resultArea.appendText("解除指导关系成功！\n");
    	}
    }

    @FXML
    void addTea(ActionEvent event) throws Exception {
    	String id = addTeacherIdTF.getText();
    	String name = addTeacherNameTF.getText();
    	if(id.equals("") || name.equals("")) {
    		resultArea.appendText("教师姓名或ID不能为空！\n");
    		return;
    	}
    	Teacher tea = new Teacher(id, name);
    	teaDAO.addTeacher(tea);
    	session.commit();
    	resultArea.appendText("插入教师成功！\n");
    }
    

    @FXML
    void addAdv(ActionEvent event) throws Exception {
    	String stu_id = addAdvisorStudentIdTF.getText();
    	String tea_id = addAdvisorTeacherIdTF.getText();
    	if(stu_id.equals("") || tea_id.equals("")) {
    		resultArea.appendText("学生或教师ID不能为空！\n");
    		addAdvisorStudentIdTF.clear();
    		addAdvisorTeacherIdTF.clear();
    		return;
    	}
    	Advisor new_adv = new Advisor(stu_id, tea_id);
		advDAO.addAdvisor(new_adv);
		session.commit();
		resultArea.appendText("新增指导关系成功！\n");
		addAdvisorStudentIdTF.clear();
		addAdvisorTeacherIdTF.clear();
    }

    @FXML
    void initialize() throws Exception {
        assert addTeacherIdTF != null : "fx:id=\"addTeacherIdTF\" was not injected: check your FXML file 'GUI.fxml'.";
        assert addStudentBtn != null : "fx:id=\"addStudentBtn\" was not injected: check your FXML file 'GUI.fxml'.";
        assert modifyTeacherIdTF != null : "fx:id=\"modifyTeacherIdTF\" was not injected: check your FXML file 'GUI.fxml'.";
        assert resultArea != null : "fx:id=\"reasultArea\" was not injected: check your FXML file 'GUI.fxml'.";
        assert addAdvisorTeacherIdTF != null : "fx:id=\"addAdvisorTeacherIdTF\" was not injected: check your FXML file 'GUI.fxml'.";
        assert findTeacherByIdBtn != null : "fx:id=\"findTeacherByIdBtn\" was not injected: check your FXML file 'GUI.fxml'.";
        assert addTeacherNameTF != null : "fx:id=\"addTeacherNameTF\" was not injected: check your FXML file 'GUI.fxml'.";
        assert addStudentIdTF != null : "fx:id=\"addStudentIdTF\" was not injected: check your FXML file 'GUI.fxml'.";
        assert deleteTeacherBtn != null : "fx:id=\"deleteTeacherBtn\" was not injected: check your FXML file 'GUI.fxml'.";
        assert findAllAdvisorsBtn != null : "fx:id=\"findAllAdvisorsBtn\" was not injected: check your FXML file 'GUI.fxml'.";
        assert addTeacherBtn != null : "fx:id=\"addTeacherBtn\" was not injected: check your FXML file 'GUI.fxml'.";
        assert findAllTeachersBtn != null : "fx:id=\"findAllTeachersBtn\" was not injected: check your FXML file 'GUI.fxml'.";
        assert delStudentByIdTF != null : "fx:id=\"delStudentByIdTF\" was not injected: check your FXML file 'GUI.fxml'.";
        assert modifyStudentIdBtn != null : "fx:id=\"modifyStudentIdBtn\" was not injected: check your FXML file 'GUI.fxml'.";
        assert findAllStudentsBtn != null : "fx:id=\"findAllStudentsBtn\" was not injected: check your FXML file 'GUI.fxml'.";
        assert findStudentIdTF != null : "fx:id=\"findStudentIdTF\" was not injected: check your FXML file 'GUI.fxml'.";
        assert deleteStudentBtn != null : "fx:id=\"deleteStudentBtn\" was not injected: check your FXML file 'GUI.fxml'.";
        assert findStudentByIdBtn != null : "fx:id=\"findStudentByIdBtn\" was not injected: check your FXML file 'GUI.fxml'.";
        assert addAdvisorStudentIdTF != null : "fx:id=\"addAdvisorStudentIdTF\" was not injected: check your FXML file 'GUI.fxml'.";
        assert modifyStudentIdTF != null : "fx:id=\"modifyStudentIdTF\" was not injected: check your FXML file 'GUI.fxml'.";
        assert deTeacherByNameTF != null : "fx:id=\"deTeacherByNameTF\" was not injected: check your FXML file 'GUI.fxml'.";
        assert studentReleaseBtn != null : "fx:id=\"studentReleaseBtn\" was not injected: check your FXML file 'GUI.fxml'.";
        assert teacherReleaseBtn != null : "fx:id=\"teacherReleaseBtn\" was not injected: check your FXML file 'GUI.fxml'.";
        assert findTeacherIdTF != null : "fx:id=\"findTeacherIdTF\" was not injected: check your FXML file 'GUI.fxml'.";
        assert addStudentNameTF != null : "fx:id=\"addStudentNameTF\" was not injected: check your FXML file 'GUI.fxml'.";
        assert addAdvisorBtn != null : "fx:id=\"addAdvisorBtn\" was not injected: check your FXML file 'GUI.fxml'.";
        assert modifyTeacherIdBtn != null : "fx:id=\"modifyTeacherIdBtn\" was not injected: check your FXML file 'GUI.fxml'.";

        init();
    }
    
    @FXML
    public void exitApplication(ActionEvent event) {
    	Platform.exit();
    }
    
}
